$(function () {
    //弹出layui
    $('#add_form').on('click', function () {
        layer.open({
            title: '添加员工信息',
            with: 300,
            height: 600,
            type: 1,
            area: '500px',
            closeBtn: 2, //不显示关闭按钮
            anim: 2,
            skin: 'layui-layer-lan',
            shadeClose: false, //开启遮罩关闭
            content: $('#zzz')

        });
    });
    /*$('.update').on('click', function () {
        layer.open({
            title: '修改员工信息',
            with: 300,
            height: 600,
            type: 1,
            area: '500px',
            closeBtn: 2, //不显示关闭按钮
            anim: 2,
            skin: 'layui-layer-lan',
            shadeClose: false, //开启遮罩关闭
            content: $('#zzz2')

        });
    });*/
    formInit();
    $('.error').parent().parent().css('margin-bottom','0px');
});

//表单验证
function formInit() {
    var validateFrom = $('#addForm');
    validateFrom.validate({
        rules: {
            name: {
                required: true,
                'isName': true,
                rangelength: [2, 5],
                remote: {   //验证昵称是否存在
                    //headers : {'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')},
                    url: '/employee/check_name',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        name: function () {
                            return $('#username').val();
                        },
                        '_token': $('meta[name="csrf-token"]').attr('content')
                    }
                }

            },
            tel: {
                required: true,
                'isMobile': true,
                remote: {   //验证电话是否存在
                    //headers : {'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')},
                    url: '/employee/check_tel',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        name: function () {
                            return $('#usertel').val();
                        },
                        '_token': $('meta[name="csrf-token"]').attr('content')
                    }
                }

            },
            birth: 'required',
            post: 'required',
            department: 'required',

        },
        messages: {
            name: {
                required: '请输入姓名',
                'isName': '名字只能是汉字',
                rangelength: '名字长度不正确',
                remote: '名字已存在',
            },
            tel: {
                'required': "请输入联系电话",
                'isMobile': "联系电话格式不正确",
                remote: '电话已存在',
            },
            birth: '请输入生日',
            post: '请输入职位',
            department: '请输入所属部门',

        }
    });

    jQuery.validator.addMethod("isMobile", function (value, element) {
        var reg = /^1[3578]\d{9}$/;
        return this.optional(element) || (reg.test(value));
    }, "手机格式不正确");

    jQuery.validator.addMethod("isName", function (value, element) {
        //var reg = /(.+)?\d(.+)?/;
        //return this.optional(element) || (!reg.test(value));
        var reg =/^[\u2E80-\u9FFF]+$/;
        return this.optional(element) || (reg.test(value));
    }, "姓名只能是汉字");
}

//修改表单数据
function upd(id){
    $.ajax({
        type: 'post',
        headers : {'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')},
        url: '/employee/update_employee/' + id,
        dataType: 'json',
        success: function (json) {
            if (json.code == '1000') {
                layer.open({
                    title: '修改员工信息',
                    with: 300,
                    height: 600,
                    type: 1,
                    area: '500px',
                    closeBtn: 2, //不显示关闭按钮
                    anim: 2,
                    skin: 'layui-layer-lan',
                    shadeClose: false, //开启遮罩关闭
                    content: json.data
                });
            } else {
                layer.msg(json.msg);
            }
        }
    })
}



